(function($){
	$(function(){
		/************登录*************/
		var userName=getCookie('username');
		if(userName!=''){
			$('#login').html('欢迎您！ '+userName+' <a href="javascript:;"> [ 退出  ] </a>');
			$('#login a').click(function(){
				$('#login').html('嘿，欢迎来顺丰优选！<a href="login.html">请登录 </a><a href="reg.html"> 免费注册</a>');
			});
		}
		/*************************************地址***********************************/
		var 
			divSheng=$('.dis_city ul .add_sheng div'),
			divShi=$('.dis_city ul .add_shi div '),
			divQu=$('.dis_city ul .add_qu div ');
		$('.dis_address').mouseenter(function(){
			$('.dis_city').addClass('hover');
			divQu.css('display','none');
			divShi.css('display','none');
			divSheng.css('display','block');
			$('.add_sheng').css({
				borderBottom:0,
			});
		}).mouseleave(function(){
			$('.dis_city').removeClass('hover');
		});
		
		$('.li_close').click(function(){
			$('.dis_city').removeClass('hover');
		});
		$('.dis_city li').each(function(i){
			if(i<3){
				$('.dis_city li').eq(i).click(function(){
					$('.dis_city li').css('border-bottom','2px solid #e8e8e8').eq(4).css('border-bottom','0');
					$(this).css('border-bottom','0');
					$('.dis_city li div').css('display','none').eq(i).css('display','block');
				})
			}else{
				return false;
			}
		});
		var shengName=$('.add_sheng div p a');
		var shiName=$('.add_shi div p a');
		var quName=$('.add_qu div p a');
		shengName.each(function(i){
			$(this).click(function(){
				var text=$('.add_sheng div p a').eq(i).html();
				$('.dis_address span').eq(0).html(text);
				$('.add_sheng span').html(text);
				
			});
		})
		shiName.each(function(i){
			$(this).click(function(){
				var text=$('.add_shi div p a').eq(i).html();
				$('.add_shi span').html(text);
			});
		});
		quName.each(function(i){
			$(this).click(function(){
				var text=$('.add_qu div p a').eq(i).html();
				$('.add_qu span').html(text);
			});
		});
		$('.add_qu p a').click(function(){
			var addSheng=$('.add_sheng span').text();
			var addShi=$('.add_shi span').text();
			var addQu=$('.add_qu span').text();
			$('.car_place').html(addSheng+addShi+addQu);
		});
		/*****************购物车****************/
		function setData(){
			var sHtml='';
			var goodMess=getCookie('mess');
			if(goodMess==''||goodMess==null||goodMess=='[]'){
				$('.car_price').css('display','none')
				$('.car_content').css('display','none');
				$('.car_kong').css('display','block');
			}else{
				$('.car_price').css('display','block')
				$('.car_content').css('display','block');
				$('.car_kong').css('display','none');
				var arr=JSON.parse(goodMess);
				for(var i=0;i<arr.length;i++){
					var goodName=unescape(arr[i].name);
					var goodPrice=arr[i].price;
					var goodSrc=arr[i].src;
					var goodNum=parseInt(arr[i].num);
					var goodSmall=goodNum*goodPrice;
					var id=arr[i].id;
					if(id!=8){
						goodSrc='../'+goodSrc;
					}
					sHtml+="<div class='thing'><div class='c_check'><input type='checkbox'  class='check' checked='checked'/></div><div class='c_detail'><dl><dt><img src='"+goodSrc+"'></dt><dd><a href='xiangxi.html'>"+goodName+"</a></dd></dl></div><div class='c_price'><b>￥</b><span>"+goodPrice+"</span></div><div class='c_youhui'></div><div class='c_num'><p class='c_jian'>-</p><input type='text'  value='"+goodNum+"'/><p class='c_jia'>+</p></div><div class='c_zhong'><span>"+goodNum+"<span>kg</div><div class='c_small'><b>￥</b><span>"+goodSmall+"</span></div><div class='c_kucun'>现货</div><div class='c_operate'><span>收藏 </span><span class='c_delete'> 删除</span></div><div class='car_mask'></div></div>";
					$('.thing_wrap').html(sHtml);
				}
			}
		}
		/******************************初始化*******************/
		setData();
		priceWeight();
		var arr=JSON.parse(getCookie('mess'));
		/*************************商品取消勾选************************/
		$('.thing .check').each(function(i){
			$(this).click(function(){
				if($(this).attr('checked')=='checked'){
					$(this).attr('checked',true);
					$('.car_mask').eq(i).css('display','none');
				}else{
					$(this).removeAttr('checked');
					$('.car_mask').eq(i).css('display','block');
				}
			});
		});
		/***************全选*****************/
		$('.all_select input').click(function(){
			if($('.all_select input').attr('checked')=='checked'){
				$('.check').attr('checked',true);
				$('.all_select input').attr('checked',true);
				$('.car_mask').css('display','none');
				priceWeight();
			}else{
				$('.check').removeAttr('checked');
				$('.all_select input').removeAttr('checked');
				$('.car_mask').css('display','block');
				
			}
		});
		/*********修改数量**************/
		$('.c_num input').each(function(i){
			$(this).blur(function(){
				var shuliang=$('.c_num').eq(i).children('input').val();
				var danjia=parseInt($('.c_price span').eq(i).text());
				arr[i].num=shuliang;
				document.cookie="mess="+JSON.stringify(arr)+";path=/";
				$('.c_num').eq(i).children('input').val(shuliang);
				$('.c_zhong').eq(i).children('span').html(shuliang);
				$('.c_small').eq(i).children('span').html(danjia*shuliang);
				priceWeight();
				
			});
		});
		/**************加**************/
		$('.c_jia').each(function(i){
			$(this).click(function(){
				var shuliang=$('.c_num').eq(i).children('input').val();
				var danjia=parseInt($('.c_price span').eq(i).text());
				arr[i].num=shuliang;
				document.cookie="mess="+JSON.stringify(arr)+";path=/";
				$('.c_num').eq(i).children('input').val(++shuliang);
				$('.c_small').eq(i).children('span').html(danjia*shuliang);
				priceWeight();
			});
		});
		/*****************减*****************/
		$('.c_jian').each(function(i){
			$(this).click(function(){
				var shuliang=$('.c_num').eq(i).children('input').val();
				var danjia=parseInt($('.c_price span').eq(i).text());
				if(shuliang<2){
					shuliang=2;
				}
				$('.c_num').eq(i).children('input').val(--shuliang);
				$('.c_small').eq(i).children('span').html(danjia*shuliang);
				arr[i].num=shuliang;
				document.cookie="mess="+JSON.stringify(arr)+";path=/";
				priceWeight();
			});
			
		});
		
		/*********************删除******************/
		$('.delete_car').click(function(){
			clearData();
		})
		var selectedNum=0;
		$('.delete_selected').click(function(){
			$('.thing .check').each(function(i){
				if($(this).attr('checked')=='checked'){
					selectedNum++;
					singleDele(i);
				}
			});
			window.location.reload();
		});
		$('.c_delete').each(function(i){
			$(this).click(function(){
				singleDele(i);
				setData();
				window.location.reload();
			});
		});
		function clearData(){
			document.cookie = "mess=" +"" + ";-1 ;path=/";
			window.location.reload();
			setData();
		}
		function singleDele(i){
			arr.splice(i,1);
			document.cookie="mess="+JSON.stringify(arr)+";path=/";
			
		}
		function priceWeight(){
			var sum=0;
			var weight=0;
			$('.c_small').each(function(i){
				sum+=parseInt($('.c_small span').eq(i).text());
			});
			$('.car_price span b').text(sum);
			$('.car_jisuan b').text(sum);
			$('.c_zhong').each(function(i){
				weight+=parseInt($('.c_zhong').eq(i).children('span').text());
			});
			$('.car_jisuan span').text(weight);
		}
		function getCookie(name){
			var str=document.cookie;
			var arr=str.split("; ");
			for(var i=0;i<arr.length;i++){
				var address=arr[i].indexOf("=");
				var name1=arr[i].slice(0,address);
				var value=arr[i].slice(address+1,arr[i].length);
				if(name==name1){
					return value;
				}
			}
			return "";
		}	
		
		
		
		
		
		
		
		
		
		
		
		
	})
})(jQuery);
